<?php

namespace App\Http\Controllers;

use App\Models\GroupSignUp;
use App\Models\GroupStart;
use App\Models\HouseList;
use App\Tools\JsonTools;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class HousePurchase extends Controller
{
    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/23
     * Time:15:35
     * @return array
     * 获取所有房源
     */
    public function House()
    {
        $data = HouseList::select('id', 'house_logo', 'house_name', 'house_title', 'house_address', 'house_price', 'square_price')->get()->toArray();
        return JsonTools::JsonSuccess($data);
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/23
     * Time:19:32
     * @param Request $request
     * @return array|void
     * 创建团购
     */
    public function Group(Request $request)
    {
        $houseId = $request->post('house_id');
        $data = [
            'house_id' => $houseId,
            'house_logo' => $request->post('house_logo'),
            'house_tittle' => $request->post('house_tittle'),
            'house_address' => $request->post('house_address'),
            'start_date' => $request->post('start_date'),
            'stop_date' => $request->post('stop_date'),
            'group_introduce' => $request->post('group_introduce'),
            'broker_id' => $request->id,
            'number' => 0,
            'state' => 0,
            'group_state' => 0
        ];
        $examine = \App\Models\HousePurchase::where('house_id', $houseId)->first();
        if ($examine) {
            return ['code' => 2002, 'msg' => '该房源已存在', 'data' => null];
        }
        $create = \App\Models\HousePurchase::create($data);
        if ($create) {
            return JsonTools::JsonSuccess($data);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/23
     * Time:19:48
     * @param Request $request
     * @return array
     * 团购列表展示
     */
    public function groupList(Request $request)
    {
        $broker = $request->id;
        $data = \App\Models\HousePurchase::where('broker_id', $broker)->get()->toArray();
        if ($data) {
            return JsonTools::JsonSuccess($data);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/24
     * Time:10:18
     * @param Request $request
     * @return array
     * 切换团购状态
     */
    public function stateSwitch(Request $request)
    {
        $id = $request->post('id');
        $state = $request->post('state');
        if ($state == 1) {
            $data = \App\Models\HousePurchase::where('id', $id)->update(['state' => 0]);
            return JsonTools::JsonSuccess($data);
        } else {
            $data = \App\Models\HousePurchase::where('id', $id)->update(['state' => 1]);
            return JsonTools::JsonSuccess($data);
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/24
     * Time:14:06
     * @param Request $request
     * @return array
     * 团购详情
     */
    public function groupDetails(Request $request)
    {
        $id = $request->post('id');
        $detail = \App\Models\HousePurchase::where('id', $id)->first();
        if ($detail) {
            return JsonTools::JsonSuccess($detail);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/25
     * Time:10:52
     * @param Request $request
     * @return array
     * 删除团购活动
     */
    public function groupDelete(Request $request)
    {
        $id = $request->post('id');
        $delete = \App\Models\HousePurchase::where('id', $id)->delete();
        if ($delete) {
            return JsonTools::JsonSuccess($delete);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/25
     * Time:16:05
     * @param Request $request
     * @return array
     * 开团
     */
    public function startGroup(Request $request)
    {
        $groupId = $request->post('group_id');
        $data = [
            'group_id' => $groupId,
            'aggregate_address' => $request->post('aggregate_address'),
            'license_plate_number' => $request->post('license_plate_number'),
            'aggregate_datetime' => $request->post('aggregate_datetime'),
            'open_group_time' => date('Y-m-d H:i:s')
        ];
        $checked = GroupStart::where('group_id', $groupId)->first();
        if ($checked) {
            return ['code' => 2002, 'msg' => '该团已开启', 'data' => null];
        }
        $state = \App\Models\HousePurchase::where('id', $groupId)->first();
        if ($state['state'] == 0) {
            return ['code' => 2003, 'msg' => '该团未在客户端显示', 'data' => null];
        }
        $start = GroupStart::create($data);
        if ($start) {
            \App\Models\HousePurchase::where('id', $groupId)->update(['group_state' => 1]);
            return JsonTools::JsonSuccess($start);
        } else {
            return JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/25
     * Time:19:13
     * @param Request $request
     * @return array
     * 客户端团购列表展示
     */
    public function clinetList(Request $request){
        $uid=$request->id;
        $list=\App\Models\HousePurchase::where('state',1)->get()->toArray();
        if ($list){
            return JsonTools::JsonSuccess($list);
        }else{
            return  JsonTools::JsonError();
        }
    }

    /**
     * Notes:
     * User: 齐硕
     * Date: 2022/11/25
     * Time:19:34
     * @param Request $request
     * 报名加入团购
     */
    public function clientJoin(Request $request){
        $uid=$request->id;
        $group_id=$request->get('group_id');
        $data=[
            'user_id'=>$uid,
            'group_id'=>$group_id,
            'state'=>1
        ];
        $user=GroupSignUp::where('user_id',$uid)->first();
        if ($user){
            return  ['code'=>2002,'msg'=>'你已经报名过','data'=>$user];
        }
        $add=GroupSignUp::create($data);
        if ($add){
           DB::table('house_purchase')->where('id',$group_id)->increment('number',1);
            return JsonTools::JsonSuccess($add);
        }else{
            return  JsonTools::JsonError();
        }
    }

    public function joinState(Request $request){
        $data=GroupSignUp::get()->toArray();
        return JsonTools::JsonSuccess($data);
    }
}
